import os
import sys

files = os.listdir('tests')
total = 0
success = 0
import_failed = 0

filter = ''
if len(sys.argv) > 1:
    filter = sys.argv[1]
    
for file in files:
    name, ext = os.path.splitext(file)
    if ext != '.py' or not name.startswith('test_'):
        continue
    if filter and not filter in name:
        continue
    try:
        module = __import__('tests.' + name, fromlist=['tests'])
        for id in dir(module):
            if id.startswith('test_'):
                method = getattr(module, id)
                try:
                    total += 1
                    method()
                    print("Test %s:%s succeeded" % (name, id[5:]))
                    success += 1
                except:
                    print("Test %s:%s failed" % (name, id[5:]))
    except:
        import_failed += 1
        print("Test %s: Import failed" % name)
    
print("%s of %s tests passed" % (success, total))
print("%s files failed to import" % import_failed)